const Service = require('egg').Service;
const jwt = require('jsonwebtoken');
// 用户信息 + 密钥 + 过期时间

// 类名字  文件夹名字 + 功能名字
// 查询数据库
class UserService extends Service {
  async login() {
    const { app, ctx } = this;
    const { username, password } = ctx.request.body; // post参数
    // egg-mysql 第三方包  app.mysql  
    const data = await app.mysql.query(`select * from users where 
    username=? and password=?`, [username, password]);
    // data = [{ id: 1, username: 'devin', password: '123' }] {...data[0]}
    const token = jwt.sign({ ...data[0] }, app.config.keys, { expiresIn: '24h' })
    return { code: 0, msg: 'success', token }
  }
}

module.exports = UserService;